﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Data;

namespace SqliteWrapper.Query
{
    public class Q_Category:IQuery
    {
        public override SqliteError Query(out DataTable DataSource, string DBPath, List<SQLiteParameter> parameters, int? startIndex, int? length, DateTime? startTime, DateTime? endTime,QueryOrder order)
        {
            DataSource = null;
            SqliteError error = new SqliteError() { ErrorText = null, IsProcessSuccess = true };
            SqliteConnectionManager manager = SqliteConnectionManager.Create(DBPath);
            using (SQLiteWriteLock locker = new SQLiteWriteLock(manager.ConnectionString))
            {
                using (SQLiteConnection conn = manager.GetSqliteConnection())
                {
                    conn.Open();
                    using (SQLiteCommand query = conn.CreateCommand())
                    {
                        QueryBuilder q_builder = new QueryBuilder("Category", "ID", "Title", "Desc", "CreateTimeTag")
                        {
                            StartIndex = startIndex,
                            Length = length,

                            StartTime = startTime,
                            EndTime = endTime,
                            Parameters = parameters,
                            OrderBy = order
                        };
                        query.CommandText = q_builder.BuildSelect();
                        try
                        {
                            using (SQLiteDataReader dr = query.ExecuteReader())
                            {
                                DataSource = q_builder.GetResult(dr);
                            }
                            error.IsProcessSuccess = true;
                        }
                        catch (Exception ex)
                        {
                            error.ErrorText = ex.Message;
                        }
                    }
                }
            }
            return error;
        }
    }
}
